package com.ruoyi.web.controller.tool;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.enums.HttpMethod;
import com.ruoyi.robot.domain.UserChatData;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;

//发送消息工具类
@Component
public class SendMsg {

    public static JSONObject postMsg(String URL,Object obj)
    {
        RestTemplate restTemplate = new RestTemplate();
        HttpMethod httpMethod = HttpMethod.POST;
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Object> entity = new HttpEntity<>(obj,httpHeaders);

        JSONObject resource = restTemplate.postForObject(URL,entity,JSONObject.class);
        return resource;
    }

    public static UserChatData setMsgToChatEntity(JSONObject srcMsg)
    {
        UserChatData userChatData = new UserChatData();
        try
        {
            userChatData.setName(srcMsg.getString("name"));
            userChatData.setNameId(srcMsg.getString("nameId"));
            userChatData.setToName(srcMsg.getString("toName"));
            userChatData.setToId(srcMsg.getString("toId"));
            userChatData.setContent(srcMsg.getString("content"));
            userChatData.setReply(srcMsg.getString("reply"));
            userChatData.setUserfile(srcMsg.getString("userfile"));
            userChatData.setRobotfile(srcMsg.getString("robotfile"));
            userChatData.setUserfilename(srcMsg.getString("userfilename"));
            userChatData.setRobotfilename(srcMsg.getString("robotfilename"));
            userChatData.setFlag(srcMsg.getString("flag"));
            userChatData.setMsgtype(srcMsg.getString("msgtype"));
            userChatData.setPosition(srcMsg.getString("position"));
            userChatData.setCreateTime(new Date());

        }catch(Exception e)
        {
            e.printStackTrace();
            return userChatData;
        }

        return userChatData;
    }

    public static String formatDataByWrap(String data)
    {
        String result = new String();
        try
        {
            for(int lg = 0;lg < data.length();lg++)
            {
                result += data.charAt(lg) + "\n";
            }
        }catch(Exception e)
        {
            e.printStackTrace();
            return result;
        }
        return result;
    }

}
